//
//  HSLeftViewController.swift
//  SchoolDemo
//
//  Created by rimi on 16/9/27.
//  Copyright © 2016年 YQ. All rights reserved.
//

import UIKit
import SnapKit
class HSLeftViewController: UIViewController {
    ///头像宽度
    @IBOutlet weak var leftIconConstraint: NSLayoutConstraint!
    ///分数分级视图

    @IBOutlet weak var LeftScroreBgView: UIView!
    ///表现分
    @IBOutlet weak var leftScoreL: UILabel!
    ///学校名称
    @IBOutlet weak var leftschoolNameL: UILabel!
    ///班级名称
    @IBOutlet weak var leftclassNameL: UILabel!
    ///账号
    @IBOutlet weak var leftaccountL: UILabel!
    ///用户名称
    @IBOutlet weak var leftNameL: UILabel!
    ///头像
    @IBOutlet weak var Lefticonview: UIImageView!
    

    @IBOutlet var leftBtnArray: [UIButton]!


    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setUI()
       
    }
    @IBAction func LeftBtnArray(_ sender: UIButton) {
        switch sender.currentTitle! {
        case "课程表":
            print("点击了课程表")
            present(HSClassTableController(), animated: true, completion: nil)
        case "关于我们":
            print("点击了关于我们")
          let abus = AboutUsViewController()
            present(abus, animated: true, completion: nil)
        case "意见反馈":
            print("点击了意见反馈")
            let suggest = suggestionController()
            present(suggest, animated: true, completion: nil)
        case "退出":
            print("点击了退出")
            //弹窗确认是否退出
            let alertVc = UIAlertController(title: "确定要退出吗？", message: nil, preferredStyle: .alert)
         
            //添加确定按钮
            alertVc.addAction(UIAlertAction(title: "确定", style: .default, handler: { (_) in
                
            
            //获得登录页面控制器
            let loginVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier:"LoginViewController")
            UIApplication.shared.keyWindow?.rootViewController = loginVc
            //修改登录状态
          UserDefaults.standard.set(false, forKey: islogin)
            }))
            //取消按钮
            alertVc.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
            
            present(alertVc, animated: true, completion: nil)
        default:
            break
    }
    }
  
    
    
    
    fileprivate func setUI() {
        //调整头像大小
        leftIconConstraint.constant = screenW <= 320 ? 60 : 80
        //设置头像圆角
        Lefticonview.layer.cornerRadius = leftIconConstraint.constant / 2
        Lefticonview.layer.masksToBounds = true
        //设置边框
        Lefticonview.layer.borderWidth = 2
        Lefticonview.layer.borderColor = UIColor.orange.cgColor
        //设置背景
        view.backgroundColor = #colorLiteral(red: 0.9999960065, green: 1, blue: 1, alpha: 1)
        //设置左边按钮的高亮背景
        for btn in leftBtnArray {
            //获得背景图片
            let image = UIImage.xx_image(color:#colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0), size: btn.bounds.size)
            
            btn.setBackgroundImage(image, for: UIControlState.highlighted)
        }
        //获得保存的用户信息数据
        let userInfo = NSDictionary(contentsOfFile: saveUserInfoPath)?["data"] as? NSDictionary
        //头像
        if let iconStr = userInfo?["studentphoto"] as? String {
        let iconData = Data(base64Encoded: iconStr, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)
        Lefticonview.image = UIImage(data: iconData!)
            
        }
        //姓名
        leftNameL.text = (userInfo?["studentName"] as? String ?? "") + "    家长"
        //账号
        leftaccountL.text = userInfo?["studentNum"] as? String
        //班级
        leftclassNameL.text = userInfo?["classname"] as? String
        //学校名称
        leftschoolNameL.text = userInfo?["schoolname"] as? String
        //成绩
        leftScoreL.text = userInfo?["studentScore"] as? String
        /*
         address = "*";
         classid = 1071394;
         classname = "\U4e94(1)\U73ed";
         grade = "\U4e94\U5e74\U7ea7";
         id = 10;
         key = 10;
         qq = "*";
         schoolname = "\U9f99\U817e\U4e91\U6559\U80b2\U79d1\U6280";
         sex = "\U5973";
         studentName = "\U738b\U6653\U5b87";
         studentNum = 5110;
         studentScore = 1;
         studentphoto =
         */
        
        let score = Int(leftScoreL.text ?? "") ?? 0
        //获得保存的用户信息
        var scoreRank: Int = 0
        //判断分数比值
        switch score {
        case 0..<10:
        scoreRank = 0
        case 10..<50:
        scoreRank = 1
        case 50..<150:
        scoreRank = 2
        case 150..<350:
        scoreRank = 3
        case 350..<850:
        scoreRank = 4
        default:
        scoreRank = 5
        
        }
        //添加分数等级视图
        for i in 0..<6 {
            //初始化图片视图
            let imageV = UIImageView(image: UIImage(named: "jujube\(i)_dark"))
            //添加到背景视图
            imageV.tag = 99 + i
            LeftScroreBgView.addSubview(imageV)
        }

    }
    //视图的生命周期方法
    override func viewDidAppear(_ animated: Bool) {
        print(LeftScroreBgView.frame)
        for iv in LeftScroreBgView.subviews {
            let margin =  LeftScroreBgView.frame.width / 5
            let x = margin * CGFloat(iv.tag - 100) + iv.bounds.width / 2
            iv.snp.makeConstraints({ (make) in
                make.centerX.equalTo(LeftScroreBgView.snp.left).offset(x)
                
                make.bottom.equalTo(LeftScroreBgView.snp.bottom).offset(-5)
            })
            
        
        }
    
    
    }
}
